'use strict';

const cfg = global.cfg;
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const rev = require('gulp-rev');
const revCollector = require('gulp-rev-collector');
const plumber = require('gulp-plumber');
const filter = require('gulp-filter');
const requirejsOptimize = require('gulp-requirejs-optimize');
const through = require('through2');
const babel = require('gulp-babel');

/**
 * 编译javascript
 * @return
 */
function gulpJavascript() {
  // 获取require 配置文件
  // static/common之类的url路径替换成common/static的文件路径
  let distPath = cfg.distPath + 'static/js';
  let jsFilter = filter(['**/*.js', '!**/*.min.js', '!**/*-min.js'].concat(cfg.filter.js), {restore: true});

  if (cfg.options.isProduction) {
    return gulp.src(cfg.src.js)
      // 排除不需要压缩的文件
      .pipe(jsFilter)
      .pipe(uglify())
      .pipe(jsFilter.restore)
      .pipe(gulp.dest(distPath))
      .pipe(rev())
      .pipe(gulp.dest(distPath))
      .pipe(rev.manifest())
      .pipe(gulp.dest(distPath));

  } else {
    return gulp.src(cfg.src.js)
      .pipe(gulp.dest(distPath))
  }
}

/**
 * 获取require配置，并以对象形式返回
 * @return {object}     配置项
 */
function getRequireCfg() {
  let filePath = path.resolve(cfg.path.cwd, cfg.appPath + cfg.path.requireConfig),
    fileContent, result;

  if (!fs.existsSync(filePath)) {
    return result;
  }
  fileContent = fs.readFileSync(filePath, 'UTF-8');

  try {
    result = Function(" var output," +
      " requirejs = require = function() {}," +
      " define = function () {};" +
      " require.config = function (options) { output = options; };" +
      fileContent + ";" +
      " return output;")()
  } catch (err) {
  }
  // 调整baseUrl
  result.baseUrl = path.resolve(cfg.path.app) + result.baseUrl;
  // 调整map
  for (let key in result.map) {
    let mapItem = result.map[key];
    if (typeof mapItem === 'object') {
      for (let subKey in mapItem) {
        let subItem = mapItem[subKey];
        result.map[key][subKey] = subItem.replace(/^\//, '');
      }
    }
  }
  return result;
}

module.exports = gulpJavascript;
